﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BHXH.Repository;
namespace Lists
{
    public class ChiTietPhieu : DoiTuong
    {
        #region Phieu property
        public Guid Phieu
        {
            get
            {
                if (_DataRow["PhieuID"] is Guid)
                    return (Guid)_DataRow["PhieuID"];
                else return Guid.Empty;
            }
            set
            {
                _DataRow["PhieuID"] = DBNull.Value;
            }
        }
        #endregion

        #region MaCau property
        public int MaCau
        {
            get
            {
                if ((int)_DataRow["MaCau"] is int) return (int)_DataRow["MaCau"];
                else return 0;
            }
            set
            {
                _DataRow["MaCau"] = value;
            }
        }
        #endregion

        #region CauTraLoiFIX1 property
        public string CauTraLoiFIX1
        {
            get
            {
                if (_DataRow["CauTraLoiFIX1"] is string) return (string)_DataRow["CauTraLoiFIX1"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiFIX1"] = value;
            }
        }
        #endregion

        #region CauTraLoiFIX2 property
        public string CauTraLoiFIX2
        {
            get
            {
                if (_DataRow["CauTraLoiFIX2"] is string) return (string)_DataRow["CauTraLoiFIX2"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiFIX2"] = value;
            }
        }
        #endregion

        #region CauTraLoiFIX3 property
        public string CauTraLoiFIX3
        {
            get
            {
                if (_DataRow["CauTraLoiFIX3"] is string) return (string)_DataRow["CauTraLoiFIX3"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiFIX3"] = value;
            }
        }
        #endregion

        #region CauTraLoiFIX4 property
        public string CauTraLoiFIX4
        {
            get
            {
                if (_DataRow["CauTraLoiFIX4"] is string) return (string)_DataRow["CauTraLoiFIX4"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiFIX4"] = value;
            }
        }
        #endregion

        #region CauTraLoiFIX5 property
        public string CauTraLoiFIX5
        {
            get
            {
                if (_DataRow["CauTraLoiFIX5"] is string) return (string)_DataRow["CauTraLoiFIX5"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiFIX5"] = value;
            }
        }
        #endregion

        #region CauTraLoiFIX6 property
        public string CauTraLoiFIX6
        {
            get
            {
                if (_DataRow["CauTraLoiFIX6"] is string) return (string)_DataRow["CauTraLoiFIX6"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiFIX6"] = value;
            }
        }
        #endregion

        #region CauTraLoiFIX7 property
        public string CauTraLoiFIX7
        {
            get
            {
                if (_DataRow["CauTraLoiFIX7"] is string) return (string)_DataRow["CauTraLoiFIX7"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiFIX7"] = value;
            }
        }
        #endregion

        #region CauTraLoiFIX8 property
        public string CauTraLoiFIX8
        {
            get
            {
                if (_DataRow["CauTraLoiFIX8"] is string) return (string)_DataRow["CauTraLoiFIX8"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiFIX8"] = value;
            }
        }
        #endregion

        #region CauTraLoiFIX9 property
        public string CauTraLoiFIX9
        {
            get
            {
                if (_DataRow["CauTraLoiFIX9"] is string) return (string)_DataRow["CauTraLoiFIX9"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiFIX9"] = value;
            }
        }
        #endregion

        #region CauTraLoiFIX10 property
        public string CauTraLoiFIX10
        {
            get
            {
                if (_DataRow["CauTraLoiFIX10"] is string) return (string)_DataRow["CauTraLoiFIX10"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiFIX10"] = value;
            }
        }
        #endregion

        #region CauTraLoiText1 property
        public string CauTraLoiText1
        {
            get
            {
                if (_DataRow["CauTraLoiText1"] is string) return (string)_DataRow["CauTraLoiText1"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiText1"] = value;
            }
        }
        #endregion

        #region CauTraLoiText2 property
        public string CauTraLoiText2
        {
            get
            {
                if (_DataRow["CauTraLoiText2"] is string) return (string)_DataRow["CauTraLoiText2"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiText2"] = value;
            }
        }
        #endregion

        #region CauTraLoiText3 property
        public string CauTraLoiText3
        {
            get
            {
                if (_DataRow["CauTraLoiText3"] is string) return (string)_DataRow["CauTraLoiText3"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiText3"] = value;
            }
        }
        #endregion

        #region CauTraLoiText property
        public string CauTraLoiText
        {
            get
            {
                if (_DataRow["CauTraLoiText"] is string) return (string)_DataRow["CauTraLoiText"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiText"] = value;
            }
        }
        #endregion

        #region CauTraLoiText5 property
        public string CauTraLoiText5
        {
            get
            {
                if (_DataRow["CauTraLoiText5"] is string) return (string)_DataRow["CauTraLoiText5"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiText5"] = value;
            }
        }
        #endregion

        #region CauTraLoiText6 property
        public string CauTraLoiText6
        {
            get
            {
                if (_DataRow["CauTraLoiText6"] is string) return (string)_DataRow["CauTraLoiText6"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiText6"] = value;
            }
        }
        #endregion

        #region CauTraLoiText7 property
        public string CauTraLoiText7
        {
            get
            {
                if (_DataRow["CauTraLoiText7"] is string) return (string)_DataRow["CauTraLoiText7"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiText7"] = value;
            }
        }
        #endregion

        #region CauTraLoiText8 property
        public string CauTraLoiText8
        {
            get
            {
                if (_DataRow["CauTraLoiText8"] is string) return (string)_DataRow["CauTraLoiText8"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiText8"] = value;
            }
        }
        #endregion

        #region CauTraLoiText9 property
        public string CauTraLoiText9
        {
            get
            {
                if (_DataRow["CauTraLoiText9"] is string) return (string)_DataRow["CauTraLoiText9"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiText9"] = value;
            }
        }
        #endregion

        #region CauTraLoiText10 property
        public string CauTraLoiText10
        {
            get
            {
                if (_DataRow["CauTraLoiText10"] is string) return (string)_DataRow["CauTraLoiText10"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiText10"] = value;
            }
        }
        #endregion

        #region GhiChu property
        public string GhiChu
        {
            get
            {
                if (_DataRow["GhiChu"] is string) return (string)_DataRow["GhiChu"];
                return "";
            }
            set
            {
                _DataRow["GhiChu"] = value;
            }
        }
        #endregion

        public ChiTietPhieu(DataRow dataRow)
            : base(dataRow)
        {
            //this.TrangThai = true;
        }
    }

    public class DanhMucChiTietPhieu : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucChiTietPhieu _Instance;
        public static DanhMucChiTietPhieu Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucChiTietPhieu();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "ChiTietPhieu";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new ChiTietPhieu((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public ChiTietPhieu this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new ChiTietPhieu(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public ChiTietPhieu this[int indexView, bool a]
        {
            get
            {
                if (0 <= indexView && indexView <= this.Count)
                {
                    return new ChiTietPhieu(DataView.ToTable().Rows[indexView]);
                }
                else
                {
                    return null;
                }
            }
        }

        public ChiTietPhieu NewDoiTuong()
        {
            return new ChiTietPhieu(DataTable.NewRow());
        }

        public ChiTietPhieu GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new ChiTietPhieu(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public ChiTietPhieu GetByID(Guid objID)
        {
            return GetByProperty("ID", objID);
        }

        public static DanhMucChiTietPhieu GetAll()
        {
            return Instance;
        }

        public static DanhMucChiTietPhieu Filter(string filterExpression)
        {
            try
            {
                DanhMucChiTietPhieu obj = new DanhMucChiTietPhieu();
                obj.DataView = new DataView(DanhMucChiTietPhieu.Instance.DataTable, filterExpression,"PhieuID", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static ChiTietPhieu GetByPhieuID_MaCau(Guid phieuID, int maCau)
        {
            try
            {
                DanhMucChiTietPhieu dm = Filter(string.Format("PhieuID = '{0}' AND MaCau = {1}", phieuID, maCau));
                if (dm.Count >= 1) return dm[0, true];
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucChiTietPhieu GetByPhieuID(Guid phieuID)
        {
            try
            {
                return Filter(string.Format("PhieuID = '{0}'", phieuID));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}
